package logic

import (
	"context"
	"pro-train-hub/pkg"
	"pro-train-hub/trainrpc/trainrpc"
	"strconv"

	"pro-train-hub/api/internal/svc"
	"pro-train-hub/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type VerificationLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewVerificationLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerificationLoginLogic {
	return &VerificationLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *VerificationLoginLogic) VerificationLogin(req *types.VerficationLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	data, err := l.svcCtx.TrainRpc.VerificationCodeLogin(l.ctx, &trainrpc.VerificationCodeLoginRequest{
		Mobile:      req.Mobile,
		CaptchaCode: req.CaptchaCode,
		SmsCode:     req.SmsCode,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(data.UserId, 10))
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "登录成功",
		Code:    200,
		Data:    handler,
	}, nil
}
